<template>
  <!-- 筛选区 -->
  <div class="sub-filter">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          href="javascript:;"
          :class="{ active: !filterInfo.brandsId }"
          @click="brandsClick(undefined)"
          >全部</a
        >
        <a
          href="javascript:;"
          :class="{ active: filterInfo.brandsId === i.id }"
          v-for="i in filterInfo.brands"
          @click="brandsClick(i.id)"
          :key="i.id"
          >{{ i.name }}</a
        >
      </div>
    </div>
    <div class="item" v-for="i in filterInfo.saleProperties" :key="i.id">
      <div class="head">{{ i.name }}：</div>
      <div class="body">
        <a
          href="javascript:;"
          :class="{ active: !i.propertiesId }"
          @click="propertiesClick(i, undefined)"
          >全部</a
        >
        <a
          href="javascript:;"
          :class="{ active: i.propertiesId === item.name }"
          v-for="item in i.properties"
          @click="propertiesClick(i, item.name)"
          :key="item.id"
          >{{ item.name }}</a
        >
      </div>
    </div>
  </div>
</template>
<script>
import { categorySubFilter } from '@/api/category'
import { useRoute, onBeforeRouteLeave } from 'vue-router'
import { ref, watch } from 'vue'
export default {
  name: 'SubFilter',
  setup (props, { emit }) {
    const route = useRoute()
    const filterInfo = ref('')
    // 通过watch观察id变化 ，id一变就调用接口获取数据
    const stop = watch(
      () => route.params.id,
      () => {
        categorySubFilter(route.params.id).then(res => {
          console.log('筛选数据:', res)
          filterInfo.value = res.result
        })
      },
      {
        immediate: true
      }
    )
    // 切换路由时，移除watch侦听
    onBeforeRouteLeave(() => {
      stop()
    })
    // 通知父级你当前选择了什么，每一次选择修改都要通知父级，父级要调用接口
    // watch(
    //   () => filterInfo.value,
    //   newVal => {
    //     console.log('修改了数据:', newVal)
    //     // 侦听到数据修改了，就通知父级emit
    //     emit('change', {
    //       brandId: newVal.brandsId,
    //       attrs: newVal.saleProperties.map(item => {
    //         return {
    //           groupName: item.name,
    //           propertyName: item.propertiesId
    //         }
    //       })
    //     })
    //   },
    //   {
    //     deep: true
    //   }
    // )
    // 点击事件处理通知父组件修改了待批
    // 品牌点击
    function brandsClick (str) {
      if (filterInfo.value.brandsId !== str) {
        filterInfo.value.brandsId = str
        postToFather()
      }
    }
    // 属性点击
    function propertiesClick (obj, str) {
      if (obj.propertiesId !== str) {
        obj.propertiesId = str
        postToFather()
      }
    }

    // 通知父组件
    function postToFather () {
      emit('change', {
        brandId: filterInfo.value.brandsId,
        attrs: filterInfo.value.saleProperties.map(item => {
          return {
            groupName: item.name,
            propertyName: item.propertiesId
          }
        })
      })
    }

    return { filterInfo, brandsClick, propertiesClick }
  }
}
</script>
<style scoped lang="less">
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
